package com.taotao.web;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.dto.EasyUIResult;
import com.taotao.model.Item;
import com.taotao.model.ItemDesc;
import com.taotao.service.ItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ItemController
 * Created by heqianqian on 2017/7/5.
 */
@Controller
@RequestMapping("/item")
public class ItemController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ItemController.class);

    @Resource
    private ItemService itemService;

    @ResponseBody
    @RequestMapping("/{itemId}")
    public Item getItem(@PathVariable("itemId") Long id) {
        return itemService.findItemById(id);
    }


    /**
     * 分页查询Item
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public EasyUIResult<List<Item>> listItem(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "rows", defaultValue = "30") Integer rows) {
        return itemService.listItem(page, rows);
    }

    /**
     * 添加商品信息
     *
     * @param item 商品信息
     * @return TaotaoResult
     */
    @ResponseBody
    @RequestMapping("/save")
    public TaotaoResult saveItem(Item item, ItemDesc itemDesc) {
        System.out.println(item);
        //TODO 无法上传cid
        item.setCid(10L);
        //TODO 没有itemdesc
        TaotaoResult taotaoResult = itemService.addItem(item, itemDesc);
        System.out.println(taotaoResult);
        return taotaoResult;
    }

}
